<?php
namespace backend\components\widgets;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\helpers\Html;


class UploadSingleFileInput extends \kartik\file\FileInput
{
    private $_value;
    private $_tem = <<<TMP
        {preview}\n
        <div class="input-group {class}">\n
           {caption}\n
           <div class="input-group-btn">\n
               {remove}\n
               {cancel}\n
               {upload}\n
               {browse}\n
           </div>\n
        </div>
TMP;

    public function init()
    {
        $this->_value = Html::getAttributeValue($this->model,$this->attribute);

        $this->pluginOptions=array_merge([
            'uploadUrl'=>Url::to(['upload/common']),
            'dropZoneEnabled' => false,
    //            'showUpload' => false,
    //            'showRemove' => false,
            'allowedFileTypes' => ['image'],
            'showUploadedThumbs' => false,
//                'allowedFileExtensions' => ['jpg', 'gif','png'],
            'fileActionSettings'=>[
                'showUpload'=>false,
                'showRemove'=>false,
                'showZoom'=>false,
                'showDrag'=>false,
            ],
            'layoutTemplates'=>[
                'main1'=>$this->_tem,
            ],
            'uploadExtraData'=>[
                'attribute'=>Html::getInputName($this->model,$this->attribute),
            ],
        ],$this->pluginOptions);

          $this->pluginEvents = array_merge([
            'fileuploaded' => 'function(event, data, previewId, index) {
                        var attribute = event.currentTarget.name;
                        $("input[type=hidden][name=\'"+attribute+"\']").val(data.response.url);

                  }',
            'fileclear' => 'function(event) {
                        var attribute = event.currentTarget.name;
                        $("input[type=hidden][name=\'"+attribute+"\']").val("");
                  }',
            'filesuccessremove' => 'function(event, id) {
                        var attribute = event.currentTarget.name;
                        $("input[type=hidden][name=\'"+attribute+"\']").val("");

                  }',

        ],$this->pluginEvents);

        $this->pluginOptions['initialPreview'] = $this->_initalPreview();
        $this->pluginOptions['uploadExtraData']['filetype'] = $this->_getFileTypeByAllowed();

        parent::init();
    }

    private function _initalPreview()
    {
        if(empty($this->_value)) return [];
        $filetype = isset($this->pluginOptions['allowedFileTypes'])?$this->pluginOptions['allowedFileTypes'][0]:'';
        switch ($filetype)
        {
            case 'image':
                $result = [
                    "<img src='{$this->_value}' class='file-preview-image' style='max-height:160px;' />",
                ];
                break;
            default:
                $result = "<div class='file-preview-text'>" .
                    "<h2><i class='glyphicon glyphicon-file'></i></h2>" .
                    basename($this->_value) . "</div>";

        }
        return $result;
    }

    private function _getFileTypeByAllowed()
    {
        $filetype = isset($this->pluginOptions['allowedFileTypes'])?$this->pluginOptions['allowedFileTypes'][0]:'other';

        return $filetype;
    }
}
